﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace CrmField.Win8App.UserControls
{
    public sealed partial class DateTimePicker : UserControl
    {
        public static readonly DependencyProperty ValueProperty =
           DependencyProperty.Register(
           "Value",
           typeof(string),
           typeof(DateTimePicker),
           new PropertyMetadata(null, ChangeValue)
           );

        public string Value
        {
            get
            {
                return (string)GetValue(ValueProperty);
            }
            set
            {
                SetValue(ValueProperty, (string)value);
            }
        }

        public DateTimePicker()
        {
            this.InitializeComponent();
        }

        private static void ChangeValue(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            DateTimePicker dp = (source as DateTimePicker);
            DateTime dateTime;
            if (e.NewValue != null && DateTime.TryParse(e.NewValue.ToString(), out dateTime))
            {
                SelectItem(dp.cb_day, dateTime.Day.ToString());
                SelectItem(dp.cb_month, dateTime.Month.ToString());
                SelectItem(dp.cb_year, dateTime.Year.ToString());

                SelectItem(dp.cb_hour, dateTime.Hour.ToString());
                SelectItem(dp.cb_minute, dateTime.Minute.ToString());
            }
            else
            {
                dp.cb_hour.SelectedIndex = -1;
                dp.cb_minute.SelectedIndex = -1;
            }
        }

        private static void SelectItem(ComboBox combo, object value)
        {

            string itemName = combo.Name + "_" + value;
            ComboBoxItem comboBoxItem = combo.FindName(itemName) as ComboBoxItem;
            if (comboBoxItem != null)
            {
                combo.SelectedIndex = combo.Items.IndexOf(comboBoxItem);
            }
            else
            {
                combo.SelectedIndex = -1;
            }
        }

        private void cb_day_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                int newDay = int.Parse(((ComboBoxItem)e.AddedItems[0]).Name.Replace("cb_day_", string.Empty));
                DateTime newDateTimeValue = DateTime.Parse(this.Value);
                this.Value = (new DateTime(newDateTimeValue.Year, newDateTimeValue.Month, newDay, newDateTimeValue.Hour, newDateTimeValue.Minute, newDateTimeValue.Second)).ToString();
            }
        }

        private void cb_month_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                int newMonth = int.Parse(((ComboBoxItem)e.AddedItems[0]).Name.Replace("cb_month_", string.Empty));
                DateTime newDateTimeValue = DateTime.Parse(this.Value);
                this.Value = (new DateTime(newDateTimeValue.Year, newMonth, newDateTimeValue.Day, newDateTimeValue.Hour, newDateTimeValue.Minute, newDateTimeValue.Second)).ToString();
            }
        }

        private void cb_year_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                int newYear = int.Parse(((ComboBoxItem)e.AddedItems[0]).Name.Replace("cb_year_", string.Empty));
                DateTime newDateTimeValue = DateTime.Parse(this.Value);
                this.Value = (new DateTime(newYear, newDateTimeValue.Month, newDateTimeValue.Day, newDateTimeValue.Hour, newDateTimeValue.Minute, newDateTimeValue.Second)).ToString();
            }
        }

        private void cb_hour_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                int newHour = int.Parse(((ComboBoxItem)e.AddedItems[0]).Name.Replace("cb_hour_", string.Empty));
                DateTime newDateTimeValue = DateTime.Parse(this.Value);
                this.Value = (new DateTime(newDateTimeValue.Year, newDateTimeValue.Month, newDateTimeValue.Day, newHour, newDateTimeValue.Minute, newDateTimeValue.Second)).ToString();
            }
        }

        private void cb_minute_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                int newMinute = int.Parse(((ComboBoxItem)e.AddedItems[0]).Name.Replace("cb_minute_", string.Empty));
                DateTime newDateTimeValue = DateTime.Parse(this.Value);
                this.Value = (new DateTime(newDateTimeValue.Year, newDateTimeValue.Month, newDateTimeValue.Day, newDateTimeValue.Hour, newMinute, newDateTimeValue.Second)).ToString();
            }
        }
    }
}
